<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class GoodSpace extends Model
{
    //
    // 获取树形结构数据
    public function getTreeData()
    {
        $data = $this->order('id')->select();
        return $this->buildTree($data);
    }

    // 递归构建树
    private function buildTree($data, $parentId = 0)
    {
        $tree = [];
        foreach ($data as $node) {
            if ($node['pid'] == $parentId) {
                $node['children'] = $this->buildTree($data, $node['id']);
                $tree[] = $node;
            }
        }
        return $tree;
    }


}
